package com.cn.boot.student.service.team.impl;

import com.cn.boot.student.entity.TeamEntity;
import com.cn.boot.student.entity.TeamUserRelationEntity;
import com.cn.boot.student.entity.UserEntity;
import com.cn.boot.student.mapper.TeamMapper;
import com.cn.boot.student.mapper.TeamUserRelationMapper;
import com.cn.boot.student.service.common.impl.RedisServiceImpl;
import com.cn.boot.student.service.team.ITeamService;
import com.cn.boot.student.util.CommonResult;
import com.cn.boot.student.util.Tool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;

/***
 * 小组管理服务实现层
 * @author luosir
 * Created by luosir in 2021/3/7
 */
@Service
//开启缓存
@CacheConfig(cacheNames = "sys:team")
public class TeamServiceImpl implements ITeamService {
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private TeamUserRelationMapper teamUserRelationMapper;
    @Autowired
    private RedisServiceImpl redisService;


    /**
     * 小组列表,分页，条件查询
     *
     * @param teamEntity
     * @return
     */
    @Override
    public CommonResult findAll(TeamEntity teamEntity) {
        //得到小组总数
        Integer count = teamMapper.getCount(teamEntity);
        PageHelper.startPage(Tool.toPageNum(teamEntity.getPageNum()), Tool.toPageSize(teamEntity.getPageSize()));
        //根据教练名字查询教练的id
        UserEntity userEntity = new UserEntity();
        userEntity.setNickName(teamEntity.getCoachName());
        if (!"".equals(userEntity.getNickName())) {
            Integer coachId = teamMapper.getCoachAndLeaderId(userEntity.getNickName());
            //将教练和组长的id赋值给小组
            teamEntity.setTeamCoachId(coachId);
        }
        //查询组长的id
        userEntity.setNickName(teamEntity.getLeaderName());
        if (!"".equals(userEntity.getNickName())) {
            Integer leaderId = teamMapper.getCoachAndLeaderId(userEntity.getNickName());
            teamEntity.setTeamLeaderId(leaderId);
        }
        List<TeamEntity> list = teamMapper.findAll(teamEntity);
        if (list.size() == 0) {
            return CommonResult.failed("暂无数据请创建！！！");
        }
        List<TeamEntity> coachNameList = teamMapper.findAllCoachName();
        List<TeamEntity> leaderNameList = teamMapper.findAllLeaderName();
        PageInfo<TeamEntity> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageInfo", pageInfo);
        map.put("count", count);
        map.put("coachNameList", coachNameList);
        map.put("leaderNameList", leaderNameList);
        return CommonResult.success(map, "查询成功！！！");
    }

    /**
     * 小组添加
     *
     * @param teamEntity
     * @return
     */
    @Override
    public CommonResult add(TeamEntity teamEntity) {
        //根据小组名称查询该小组是否已经存在，若存在则给出提示
        List<TeamEntity> list = teamMapper.teamIsExist(teamEntity.getTeamName());
        if (list.size() != 0) {
            return CommonResult.success("isHave", "该小组已存在，请重新输入！！！");
        }
        //根据教练名字查询教练的id
        UserEntity userEntity = new UserEntity();
        String oldCoachName = teamEntity.getCoachName();
        userEntity.setNickName(oldCoachName);
        if (!"".equals(userEntity.getNickName())) {
            Integer coachId = teamMapper.getCoachAndLeaderId(userEntity.getNickName());
            //将教练和组长的id赋值给小组
            teamEntity.setTeamCoachId(coachId);
        }
        //查询组长的id
        String oldLeaderName = teamEntity.getLeaderName();
        userEntity.setNickName(oldLeaderName);
        if (!"".equals(userEntity.getNickName())) {
            Integer leaderId = teamMapper.getCoachAndLeaderId(userEntity.getNickName());
            teamEntity.setTeamLeaderId(leaderId);
        }
        //添加小组
        Integer result = teamMapper.add(teamEntity);
        //添加小组的同时将组长的id，小组id加入小组学员关系表中
        TeamUserRelationEntity teamUserRelationEntity = new TeamUserRelationEntity();
        teamUserRelationEntity.setUserId(teamEntity.getTeamLeaderId());
        //获取当前数据库里小组最大id，新建数据时小组id+1(自增)
        Integer maxTeamId = teamMapper.getMaxTeamId();
        teamUserRelationEntity.setTeamId(maxTeamId);
        //插入数据库中
        Integer add = teamUserRelationMapper.add(teamUserRelationEntity);
        if (add == 0) {
            return CommonResult.failed("组长加入小组失败！！！");
        }
        if (result != 0) {
            teamEntity.setCoachName(oldCoachName);
            teamEntity.setLeaderName(oldLeaderName);
            return CommonResult.success(result, "添加成功！！！");
        }
        return CommonResult.success(result, "添加失败！！！");
    }

    /**
     * 小组更新
     *
     * @param teamEntity
     * @return
     */
    @Override
    public CommonResult update(TeamEntity teamEntity) {
        Integer result = teamMapper.update(teamEntity);
        if (result != 0) {
            return CommonResult.success(result, "更新成功！！！");
        }
        return CommonResult.success(result, "更新失败！！！");
    }

    /**
     * 删除小组
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult del(Integer id) {
        Integer result = teamMapper.del(id);
        //删除小组的同时释放小组成员和组长
        teamUserRelationMapper.release(id);
        if (result != 0) {
            return CommonResult.success(result, "删除成功！！！");
        }

        return CommonResult.success(result, "删除失败！！！");
    }

    /***
     * 批量删除小组
     * @param ids
     * @return
     */
    @Override
    public CommonResult dels(List<Integer> ids) {
        ids.stream().forEach(teamMapper::del);
        return CommonResult.success(ids.size(), "成功删除" + ids.size() + "条数据");
    }

    /**
     * 小组分配成员
     *
     * @param teamEntity
     * @return
     */
    @Override
    public CommonResult assignStudent(TeamEntity teamEntity) {
        return null;
    }

    /**
     * 根据id查询小组信息
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult findById(Integer id) {
        TeamEntity teamEntity = teamMapper.findById(id);
        Assert.isTrue(teamEntity.getId() == id, "没有数据");

        return CommonResult.success(teamEntity);
    }

    /**
     * 得到所有角色为教练的用户
     *
     * @return
     */
    @Override
    public CommonResult findAllCoach() {
        List<UserEntity> list = teamMapper.findAllCoach();
        if (list.size() != 0) {
            return CommonResult.success(list);
        }
        return CommonResult.success(list, "教练空缺");
    }

    /**
     * 得到所有角色为组长的用户
     *
     * @return
     */
    @Override
    public CommonResult findAllLeader() {
        List<UserEntity> list = teamMapper.findAllLeader();
        if (list.size() != 0) {
            return CommonResult.success(list);
        }
        return CommonResult.success(list, "组长空缺");
    }

    /**
     * 得到已经分配小组的学员
     *
     * @return
     */
    @Override
    public CommonResult getIsAssignStudent(TeamEntity teamEntity) {
        Integer count = teamMapper.getIsAssignStudentCount(teamEntity);
        List<UserEntity> list = teamMapper.getIsAssignStudent(teamEntity);
        if (list.size() == 0) {
            return CommonResult.failed("当前暂无数据，请分配学员！！！");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("list", list);
        return CommonResult.success(map);
    }

    /**
     * 得到未分配小组的学员
     *
     * @return
     */
    @Override
    public CommonResult getNoAssignStudent(TeamEntity teamEntity) {
        Integer count = teamMapper.getNoAssignStudentCount(teamEntity);
        List<UserEntity> list = teamMapper.getNoAssignStudent(teamEntity);
        if (list.size() == 0) {
            return CommonResult.failed("当前暂无数据，请添加学员！！！");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("list", list);
        return CommonResult.success(map);
    }
}
